package com.haitaoit.chengquan.module.home.activity.zhaopin;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.base.ExitApplication;
import com.haitaoit.chengquan.module.home.model.RecruitCompanyModel2;
import com.haitaoit.chengquan.module.login.LoginActivity;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.GetSign;
import com.haitaoit.chengquan.utils.HttpUtilsSingle;
import com.haitaoit.chengquan.utils.NameValuePairSign;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class RecruitPersonalActivity extends BaseActivity {
    @ViewInject(R.id.contact_phone)
    TextView contact_phone;
    @ViewInject(R.id.contact_title)
    TextView contact_title;
    @ViewInject(R.id.contact_time)
    TextView contact_time;
    @ViewInject(R.id.contact_name)
    TextView contact_name;
    @ViewInject(R.id.expected_salary)
    TextView expected_salary;
    @ViewInject(R.id.education)
    TextView education;
    @ViewInject(R.id.work_year)
    TextView work_year;
    @ViewInject(R.id.expect_work_place)
    TextView expect_work_place;
    @ViewInject(R.id.work_experience)
    TextView work_experience;
    @ViewInject(R.id.work_type)
    TextView work_type;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_recruit_personal);
        setTitle_V("求职信息详情");
        setLeftShow(1, R.mipmap.left);
        setRightShow(0,R.string.bianji);
        backLeft_V();
        ViewUtils.inject(this);

        final String id = getIntent().getStringExtra("id");
        httpGetjob_information_model(id);


        //点击编辑
        getRight_V().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(ExitApplication.user_id)){
                    startActivity(new Intent(RecruitPersonalActivity.this, LoginActivity.class));
                }else {
                    Intent intent = new Intent(RecruitPersonalActivity.this,JobChengeActivity.class);

                    intent.putExtra("myJobId",id);

                    startActivity(intent);
                }
            }
        });
    }

    @OnClick({R.id.contact_phone,})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.contact_phone:// 电话
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
                    startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + contact_phone.getText().toString().substring(5))));
                }else {
                    ToastUtils.showToast("请设置读取手机电话号码的权限");
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    //        Uri uri = Uri.fromParts("package", context.getPackageName());
                    //        intent.setData(uri);
                    intent.setData(Uri.parse("package:" + getPackageName()));
                    startActivity(intent);
                }
                break;
        }
    }

    private void httpGetjob_information_model(String id) {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("id", id));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("id", id);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getjob_information_model, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    Gson gson = new Gson();
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            RecruitCompanyModel2 model = gson.fromJson(Response, RecruitCompanyModel2.class);

                            contact_title.setText(model.getTitle());
                            contact_time.setText(model.getAddtime());
                            contact_name.setText(model.getName());
                            expected_salary.setText(model.getExpected_salary());
                            education.setText("学历：" + model.getEducation());
                            work_year.setText("工作经验：" + model.getWork_year());
                            expect_work_place.setText("期望地区：" + model.getExpect_work_place());
                            contact_phone.setText("联系电话：" + model.getPhone());
                            work_experience.setText(getString(R.string.RecruitCompany_work_expe) + model.getWork_experience());
                            work_type.setText(getString(R.string.RecruitCompany_work_type) + model.getJob_category());
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }
}
